<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--动态数据-->
      <el-col :span="24" :xs="24">
      
        <!-- 搜索 -->
        <template>
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
            <el-form-item label="内容" prop="dynamicContent">
              <el-input
                v-model="queryParams.dynamicContent"
                placeholder="请输入内容"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="店铺ID" prop="shopId" v-if="$store.getters && $store.getters.admin">
              <el-input
                v-model="queryParams.shopId"
                placeholder="请输入店铺ID"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="dateRange"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </template>

        <!-- 按钮操作栏 -->
        <template>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="multiple"
                @click="handleStatusChange('approve')"
                v-hasPermi="['dynamic:dynamic:edit']"
              >批量通过</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="info"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="multiple"
                @click="handleStatusChange('reject')"
                v-hasPermi="['dynamic:dynamic:edit']"
              >批量驳回</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['dynamic:dynamic:remove']"
              >删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
        </template>

        <!-- 表格 -->
        <template>
          <el-table v-loading="loading" :data="dynamicList" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column type="index" label="序号" width="50" align="center" />
            <!-- <el-table-column label="编号" align="center" key="dynamicId" prop="dynamicId" /> -->
            <el-table-column label="内容" align="center" key="dynamicContent" prop="dynamicContent" minWidth="200px" :show-overflow-tooltip="true" />
            <el-table-column label="图片视频" align="center" key="dynamicImage" minWidth="100">
              <template slot-scope="scope">
                <div class="flex-center" v-if="scope.row.dynamicImage">
                  <el-image shape="square" size="medium" :src="v" alt="404" class="mgr5" fit="contain" style="width: 60px; height: 60px" v-for="v in scope.row.dynamicImage.split(',')" :preview-src-list="scope.row.dynamicImage.split(',')"></el-image>
                </div>
                <div class="flex-center" v-else-if="scope.row.dynamicVideo">
                  <video width="150" controls>
                    <source :src="scope.row.dynamicVideo" type="video/mp4">
                    您的浏览器不支持 video 标签。
                  </video>
                </div>
                <div v-else>-</div>
              </template>
            </el-table-column>
            <el-table-column label="店员" align="left" key="userId" min-width="120" >
              <template slot-scope="scope">
                <div class="flex-align-center">
                  <el-avatar shape="circle" size="medium" :src="scope.row.employeeUser.avatar" alt="404"></el-avatar>
                  <div class="mgl5 font-14">{{scope.row.employeeUser.nickName}}</div>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="店铺名称" prop="shopInfo.shopName" v-if="$store.getters && $store.getters.admin" />
            <el-table-column label="创建时间" align="center" prop="createTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="160">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              width="160"
              fixed="right"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['dynamic:dynamic:edit']"
                >审核</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['dynamic:dynamic:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </template>

      </el-col>
    </el-row>

    <!-- 弹窗 -->
    <template>
      <!-- 添加或修改动态配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="1200px" :close-on-click-modal="false" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="内容" prop="dynamicContent">
                <el-input v-model="form.dynamicContent" placeholder="请输入描述" type="textarea" maxlength="200" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="form.dynamicImage">
            <el-col :span="24">
              <el-form-item label="图片" prop="dynamicImage">
                <el-image shape="square" size="medium" :src="v" alt="404" class="mgr5" style="width: 100px;" v-for="v in form.dynamicImage.split(',')" :preview-src-list="form.dynamicImage.split(',')"></el-image>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="form.dynamicVideo">
            <el-col :span="24">
              <el-form-item label="视频" prop="dynamicVideo">
                <video width="150" controls>
                    <source :src="form.dynamicVideo" type="video/mp4">
                    您的浏览器不支持 video 标签。
                  </video>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="success" @click="submitForm('approve')">通 过</el-button>
          <el-button type="danger" @click="submitForm('reject')">驳 回</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      
    </template>
  </div>
</template>

<script>
import { listDynamic, getDynamic, delDynamic, updateDynamic, changeDynamicStatus } from "@/api/dynamic/dynamic";
import { getToken } from "@/utils/auth";

export default {
  name: "DynamicAudit",
  dicts: ['sys_normal_disable', 'sys_yes_no'],
  components: { },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 动态表格数据
      dynamicList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 动态导入参数
      upload: {
        // 是否显示弹出层（动态导入）
        open: false,
        // 弹出层标题（动态导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的动态数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/dynamic/dynamic/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dynamicTitle: undefined,
        status: 'audit',
        imageType: undefined,
        goType: undefined,
        navType: undefined,
        delFlag: undefined,
        shopId: undefined,
        orderByColumn: 'createTime',
        isAsc: 'descending',
      },
      // 表单校验
      rules: {
        dynamicTitle: [
          { required: true, message: "名称不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ]
      },
    };
  },
  computed: {
  },
  watch: {
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询动态列表 */
    getList() {
      this.loading = true;
      listDynamic(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.dynamicList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 动态状态修改
    handleStatusChange(status) {
      let dynamicIds = this.ids;
      let text = status === "approve" ? "通过" : status === "reject" ?  "驳回" : '未知状态';
      this.$modal.confirm('确认要"' + text + '"动态吗？').then(function() {
        return changeDynamicStatus(dynamicIds.join(","), status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.getList();
      }).catch(function() {
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dynamicId: undefined,
        dynamicTitle: undefined,
        dynamicDescription: undefined,
        dynamicSort: undefined,
        dynamicImage: undefined,
        appId: undefined,
        delFlag: undefined,
        appId: undefined,
        delFlag: undefined,
        url: undefined,
        status: 'audit',
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.dynamicId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dynamicId);
      console.log(this.ids)
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getDynamic().then(response => {
        this.open = true;
        this.title = "添加动态";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dynamicId = row.dynamicId || this.ids;
      getDynamic(dynamicId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改动态";
      });
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 提交按钮 */
    submitForm: function(status) {
      let tips = status === 'approve' ? '是否确认通过？' : status === 'reject' ? '是否驳回' : ''
      this.$modal.confirm(tips).then(function() {
      }).then(() => {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let params = JSON.parse(JSON.stringify(this.form))
            params.status = status
            updateDynamic(params).then(response => {
              this.$modal.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          }
        });
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dynamicIds = row.dynamicId || this.ids;
      this.$modal.confirm('是否确认删除动态编号为"' + dynamicIds + '"的数据项？').then(function() {
        return delDynamic(dynamicIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>